package com.cn.wanxi.util;

import com.cn.wanxi.model.DataField;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Upload {
    /**
     * @param request 根据请求解析请求中的参数(文件与非文件)返回Map集合,并将文件上传至服务器
     * @return
     */
    public static void FileUpload(HttpServletRequest request) {
        FileItemFactory factory = new DiskFileItemFactory();

        // 文件上传处理器
        ServletFileUpload upload = new ServletFileUpload(factory);

        // 解析请求信息
        List<FileItem> items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        System.out.println(items);
        // 对请求信息进行判断
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            // 信息为普通的格式
            if (item.isFormField()) {
                String fieldName = item.getFieldName();
                String value = item.getString();
                request.setAttribute(fieldName, value);
            }
            // 信息为文件格式
            else {
                String fileName = item.getName();//获得上传图片的名称
                int index = fileName.lastIndexOf("\\");
                fileName = new Date().getTime() + "-" + fileName.substring(index + 1);
                String fieldName = item.getFieldName();
                request.setAttribute(fieldName, DataField.url + fileName);
                System.out.println(DataField.basePath);//打印当前位置
                File file = new File(DataField.basePath, DataField.url + fileName);
                try {
                    item.write(file);
                    Upload.UploadOut(fileName);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }
    //将图片复制一份到out文件夹
    public static void UploadOut(String fileName) {
        File file = new File(DataField.outPath, DataField.url + fileName);
        try {
            //读取源地址文件的字节流
            FileInputStream in = new FileInputStream(DataField.basePath + DataField.url + fileName);
            FileOutputStream out = new FileOutputStream(file);
            byte[] bs = new byte[1026];
            int count = 0;
            while ((count = in.read(bs, 0, bs.length)) != -1) {
                //把读取到的字节流写入到目的地址的文件里面
                out.write(bs, 0, count);

            }
            //刷新下输出流
            out.flush();
            // 关闭输入流和输出流
            out.close();
            out.close();
            System.out.println("复制成功！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void FileUpload(HttpServletRequest request,String path,String url) {

        FileItemFactory factory = new DiskFileItemFactory();

        // 文件上传处理器
        ServletFileUpload upload = new ServletFileUpload(factory);

        // 解析请求信息
        List<FileItem> items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        System.out.println(items);
        // 对请求信息进行判断
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            // 信息为普通的格式
            if (item.isFormField()) {
                String fieldName = item.getFieldName();
                String value = item.getString();
                request.setAttribute(fieldName, value);
            }
            // 信息为文件格式
            else {
                String fileName = item.getName();//获得上传图片的名称
                int index = fileName.lastIndexOf("\\");
                fileName = new Date().getTime() + "-" + fileName.substring(index + 1);
                String fieldName = item.getFieldName();
                request.setAttribute(fieldName, url + fileName);
                System.out.println(path + url + fileName);//打印当前位置
                File file = new File(path, url + fileName);
                try {
                    item.write(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }
    //将图片从起止文件夹复制一份到目标文件夹
    public static void UploadOut(String urlFileName,String startPath,String endPath) {
        File file = new File(endPath, urlFileName);
        try {
            //读取源地址文件的字节流
            FileInputStream in = new FileInputStream(startPath + urlFileName);
            FileOutputStream out = new FileOutputStream(file);
            byte[] bs = new byte[1026];
            int count = 0;
            while ((count = in.read(bs, 0, bs.length)) != -1) {
                //把读取到的字节流写入到目的地址的文件里面
                out.write(bs, 0, count);

            }
            //刷新下输出流
            out.flush();
            // 关闭输入流和输出流
            out.close();
            out.close();
            System.out.println("复制成功！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

